/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.momarketing.service;

import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.momarketing.entity.CustomPage;
import com.boxin.ims.modules.momarketing.dao.CustomPageDao;

/**
 * 自定义网页Service
 * @author Jakemanse
 * @version 2014-03-21
 */
@Component
@Transactional(readOnly = true)
public class CustomPageService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(CustomPageService.class);
	
	@Autowired
	private CustomPageDao customPageDao;
	
	public CustomPage get(Long id) {
		return customPageDao.findOne(id);
	}
	
	
	
	public Page<CustomPage> find(Page<CustomPage> page, CustomPage customPage) {
		DetachedCriteria dc = customPageDao.createDetachedCriteria();
		if (customPage.getUser()!=null && customPage.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", customPage.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(customPage.getName())){
			dc.add(Restrictions.like("name", "%"+customPage.getName()+"%"));
		}
		dc.addOrder(Order.desc("id"));
		return customPageDao.find(page, dc);
	}
	
	
	
	
	
	public List<CustomPage> findAllByUserId(Long userId){
		DetachedCriteria dc = customPageDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		return customPageDao.find(dc);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-1  上午11:08:26
	 * @function <p>广告页 </p>
	 * @param userId
	 * @return
	 */
	public List<CustomPage> findAdvertPagesByUserId(Long userId){
		DetachedCriteria dc = customPageDao.createDetachedCriteria();
		dc.add(Restrictions.eqOrIsNull("type", CustomPage.PAGE_TYPE_ADVERT));
		dc.add(Restrictions.eq("user.id", userId));
		return customPageDao.find(dc);
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2014-4-1  上午11:07:29
	 * @function <p> 签到网页 </p>
	 * @param userId
	 * @return
	 */
	public List<CustomPage> findSignupPageByUserId(Long userId){
		DetachedCriteria dc = customPageDao.createDetachedCriteria();
		dc.add(Restrictions.eqOrIsNull("type", CustomPage.PAGE_TYPE_SIGNUP));
		dc.add(Restrictions.eq("user.id", userId));
		return customPageDao.find(dc);
	}
	
	
	
	
	
	
	
	@Transactional(readOnly = false)
	public void save(CustomPage customPage) {
		customPage.setContent(StringEscapeUtils.unescapeHtml4(customPage.getContent()) );
		customPageDao.save(customPage);
	}
	
	
	
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		customPageDao.deleteById(id);
	}
	
}
